package com.tianzj.boot.mvc.service.system.impl;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.tianzj.boot.mvc.entity.system.SysRole;
import com.tianzj.boot.mvc.repository.system.SysRoleRepository;
import com.tianzj.boot.mvc.service.system.SysRoleService;
import com.tianzj.boot.util.BeanCopyUtil;

@Service
public class SysRoleServiceImpl implements SysRoleService {

	@Autowired
	private SysRoleRepository sysRoleRepository;
	
	@Override
	public Page<SysRole> getSysRoleList(final SysRole sysRole, Pageable pageable) {
		 Specification<SysRole> specification = new Specification<SysRole>() {  
	            @Override  
	            public Predicate toPredicate(Root<SysRole> root, CriteriaQuery<?> query, CriteriaBuilder cb) {  
	                Path<String> roleName = root.get("roleName");  
	                Path<Long> status = root.get("status");  
	                Predicate predicate = null;
	                if(StringUtils.isNotBlank(sysRole.getRoleName())){
	                	  predicate =cb.and(cb.like(roleName, "%"+sysRole.getRoleName()+"%"));
	                }
	                if(sysRole.getStatus()!=null){
	                	Predicate predicate2=cb.and(cb.equal(status, sysRole.getStatus()));
	                	 predicate = getPre(cb, predicate, predicate2);
	                }
	                Predicate predicate2 = cb.notEqual(status, 9);
	                predicate = getPre(cb, predicate, predicate2);
	                return predicate;  
	            }
			 
	        };  
	    Page<SysRole> SysRoles = sysRoleRepository.findAll(specification,pageable);
		return SysRoles;
	}
	private Predicate getPre(CriteriaBuilder cb, Predicate predicate, Predicate predicate2) {
		if(predicate!=null){
			 predicate=cb.and(predicate, predicate2);
		 }else{
			 predicate=predicate2;
		 }
		return predicate;
	}
	@Override
	public void addRole(SysRole sysRole) {
		sysRoleRepository.save(sysRole);
	}
	@Override
	public void updateRole(SysRole sysRole) {
		SysRole  role= sysRoleRepository.findOne(sysRole.getId());
		BeanUtils.copyProperties(sysRole, role, BeanCopyUtil.getNullPropertyNames(sysRole));
		sysRoleRepository.save(role);
	}
	@Override
	public SysRole getSysRoleById(Long id) {
		return sysRoleRepository.findOne(id);
	}
	@Override
	@Transactional
	public void deleteSysRoleById(String ids) {
		if(StringUtils.isNotBlank(ids)){
			String[] idsArray = ids.split(","); 
			for (String id : idsArray) {
				SysRole  role= sysRoleRepository.findOne(Long.parseLong(id));
				role.setStatus(9);
				sysRoleRepository.save(role);
			}
		}
	}
	@Override
	public List<SysRole> getRoleListNoPage() {
		return sysRoleRepository.findByStatusNot(9);
	} 
}
